package com.aquarium.ui;

import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import com.aquarium.alarm.AlarmManager;
import com.aquarium.bt.BTManager;
import com.aquarium.mail.MailInfo;
import com.aquarium.mail.MailSender;
import com.aquarium.sensor.SensorManager;
import com.aquarium.util.DialogUtils;
import com.aquarium.util.NotificationUtils;
import com.aquarium.util.SPUtils;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class AlarmService extends Service implements Observer {
	private String TAG = "AlarmService";

	private SensorManager mSensorManager;
	private AlarmManager mAlarmManager;
	private MailInfo mailInfo;
	private MailSender mailSender;
	private BTManager mBTManager;
	/*
	 * private float mPhMin; private float mPhMax; private float
	 * mTemperatureMin; private float mTemperatureMax; private float mOxygenMin;
	 * private float mOxygenMax; private float mConductivityMin; private float
	 * mConductivityMax; private float mWaterLevelMin; private float
	 * mWaterLevelMax;
	 * 
	 * private int mPhAlarmInterval; private int mTemperatureAlarmInterval;
	 * private int mOxygenAlarmInterval; private int mConductivityAlarmInterval;
	 * private int mWaterLevelAlarmInterval;
	 */

	private float mCurrentPhValue;
	private float mCurrentTemperatureValue;
	private float mCurrentOxygenValue;
	private float mCurrentConductivityValue;
	private float mCurrentWaterLevelValue;

	private HashMap<String, String> mSensorValueMap;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d(TAG, "onCreate");
		mSensorManager = SensorManager.getInstance();
		mSensorManager.addObserver(this);
		mAlarmManager = AlarmManager.getInstance(this);
		// updateAlarmValue();
		// mSensorManager.notifySensorValueTest();
		mailInfo = MailInfo.getInstance(this);
		mailSender = new MailSender(this);
		mBTManager = BTManager.getInstance();
		mBTManager.sendSensorValueTest();

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mSensorManager.stopNotifySensorValueTest();
		mBTManager.stopSendSensorValueTest();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Observable observable, Object data) {
		if (observable instanceof SensorManager) {
			mSensorValueMap = (HashMap<String, String>) data;
			mCurrentPhValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.PH_VALUE_KEY));
			mCurrentTemperatureValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.TEMPERATURE_VALUE_KEY));
			mCurrentOxygenValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.OXYGEN_VALUE_KEY));
			mCurrentConductivityValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.CONDUCTIVITY_VALUE_KEY));
			mCurrentWaterLevelValue = Float.valueOf(mSensorValueMap
					.get(SensorManager.WATER_LEVEL_VALUE_KEY));
			// Log.d(TAG, "	mCurrentPhValue:" + mCurrentPhValue
			// + "		mCurrentOxygenValue:" + mCurrentOxygenValue
			// + "		mCurrentTemperatureValue:" + mCurrentTemperatureValue
			// + "		mCurrentConductivityValue:"
			// + mCurrentConductivityValue + "		mCurrentWaterLevelValue:"
			// + mCurrentWaterLevelValue);
			// displayCurrentValue();
		}
		/*
		 * else if (observable instanceof AlarmManager) { HashMap<String,
		 * String> map = (HashMap<String, String>) data; // TODO find an more
		 * efficient way to get value. if
		 * (map.containsKey(AlarmManager.GET_PH_MIN_KEY)) { mPhMin =
		 * Float.parseFloat(map.get(AlarmManager.GET_PH_MIN_KEY)); } else if
		 * (map.containsKey(AlarmManager.GET_PH_MAX_KEY)) { mPhMax =
		 * Float.parseFloat(map.get(AlarmManager.GET_PH_MAX_KEY)); } else if
		 * (map.containsKey(AlarmManager.GET_PH_MAX_KEY)) { mTemperatureMin =
		 * Float.parseFloat(map .get(AlarmManager.GET_TEMPERATURE_MIN_KEY)); }
		 * else if (map.containsKey(AlarmManager.GET_PH_MAX_KEY)) {
		 * mTemperatureMax = Float.parseFloat(map
		 * .get(AlarmManager.GET_TEMPERATURE_MAX_KEY)); } else if
		 * (map.containsKey(AlarmManager.GET_OXYGEN_MIN_KEY)) { mOxygenMin =
		 * Float.parseFloat(map .get(AlarmManager.GET_OXYGEN_MIN_KEY)); } else
		 * if (map.containsKey(AlarmManager.GET_OXYGEN_MAX_KEY)) { mOxygenMax =
		 * Float.parseFloat(map .get(AlarmManager.GET_OXYGEN_MAX_KEY)); } else
		 * if (map.containsKey(AlarmManager.GET_CONDUCTIVITY_MIN_KEY)) {
		 * mConductivityMin = Float.parseFloat(map
		 * .get(AlarmManager.GET_CONDUCTIVITY_MIN_KEY)); } else if
		 * (map.containsKey(AlarmManager.GET_CONDUCTIVITY_MAX_KEY)) {
		 * mConductivityMax = Float.parseFloat(map
		 * .get(AlarmManager.GET_CONDUCTIVITY_MAX_KEY)); } else if
		 * (map.containsKey(AlarmManager.GET_WATER_LEVEL_MIN_KEY)) {
		 * mWaterLevelMin = Float.parseFloat(map
		 * .get(AlarmManager.GET_WATER_LEVEL_MIN_KEY)); } else if
		 * (map.containsKey(AlarmManager.GET_WATER_LEVEL_MAX_KEY)) {
		 * mWaterLevelMax = Float.parseFloat(map
		 * .get(AlarmManager.GET_WATER_LEVEL_MAX_KEY)); } Log.d(TAG, "	mPhMin:"
		 * + mPhMin + "	mPhMax:" + mPhMax + "	mTemperatureMin:" +
		 * mTemperatureMin + "	mTemperatureMax:" + mTemperatureMax +
		 * "	mOxygenMin:" + mOxygenMin + "	mOxygenMax:" + mOxygenMax +
		 * "	mConductivityMin:" + mConductivityMin + "	mConductivityMax:" +
		 * mConductivityMax + "	mWaterLevelMin:" + mWaterLevelMin +
		 * "	mWaterLevelMax:" + mWaterLevelMax);
		 * 
		 * }
		 */
		else {
			Log.e(TAG, "update,error data ");
		}

		Log.d(TAG, "update,get data");
		// if (alarmInfo() != null) {
		// Log.d(TAG, alarmInfo());
		// String[] strings = alarmInfo().split(";");
		// Intent intent = new Intent();
		// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// intent.putExtra(AlarmManager.INFO, strings);
		// intent.setClass(this, AlarmDialogActivity.class);
		// startActivity(intent);
		// }
Log.d(TAG, mAlarmManager.isPhoneEnable()+"");
Log.d(TAG, mAlarmManager.isRangEnable() +"");
Log.d(TAG, mailInfo.isEmailEnable()+"");
		if (alarmInfo() != null
				&& mAlarmManager.isNeedAlarm()
				&& (mAlarmManager.isPhoneEnable() || mAlarmManager
						.isRangEnable() || mailInfo.isEmailEnable())) {
			Log.d(TAG, "start alarm ");
			 NotificationUtils.show(this, alarmInfo().split(";"));
			if (mAlarmManager.isPhoneEnable()) {
				Log.d(TAG, "start sms true");
				 mAlarmManager.sendSms(alarmInfo());
			} else {
				Log.d(TAG, "mAlarmManager.isPhoneEnable() false");
			}
			if (mAlarmManager.isRangEnable()) {
				Log.d(TAG, "start rang true");
				mAlarmManager.rang();
			} else {
				Log.d(TAG, "mAlarmManager.isRangEnable() false");
			}
			if (mailInfo.isEmailEnable()) {
				Log.d(TAG, "start mail true");

				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						mailSender.sendTextMail(alarmInfo());
					}
				}).start();

				// mailSender.sendTextMail(alarmInfo());

			} else {
				Log.d(TAG, "mailSenderInfo.isEmailEnable() false");
			}
			mAlarmManager.setAlarmTime();

		}
	}

	/*
	 * private void updateAlarmValue() { mPhMax = mAlarmManager.getPhMax();
	 * mPhMin = mAlarmManager.getPhMin(); mTemperatureMax =
	 * mAlarmManager.getTemperatureMax(); mTemperatureMin =
	 * mAlarmManager.getTemperatureMin(); mOxygenMax =
	 * mAlarmManager.getOxygenMax(); mOxygenMin = mAlarmManager.getOxygenMin();
	 * mConductivityMax = mAlarmManager.getConductivityMax(); mConductivityMin =
	 * mAlarmManager.getConductivityMin(); mWaterLevelMax =
	 * mAlarmManager.getWaterLevelMax(); mWaterLevelMin =
	 * mAlarmManager.getWaterLevelMin();
	 * 
	 * mPhAlarmInterval = mAlarmManager.getPhAlarmInterval();
	 * mTemperatureAlarmInterval = mAlarmManager.getTemperatureAlarmInterval();
	 * mOxygenAlarmInterval = mAlarmManager.getOxygenAlarmInterval();
	 * mConductivityAlarmInterval = mAlarmManager
	 * .getConductivityAlarmInterval(); mWaterLevelAlarmInterval =
	 * mAlarmManager.getWaterLevelInterval();
	 * 
	 * }
	 */

	private String alarmInfo() {
		return (mAlarmManager.alarmPhInfo(mCurrentPhValue)
				+ mAlarmManager
						.alarmConductivityInfo(mCurrentConductivityValue)
				+ mAlarmManager.alarmOxygenInfo(mCurrentOxygenValue)
				+ mAlarmManager.alarmTemperatureInfo(mCurrentTemperatureValue) + mAlarmManager
					.alarmWaterLevelInfo(mCurrentWaterLevelValue)).replaceAll(
				"null", "");
	}

}
